import java.rmi.*;
import java.rmi.Naming;
import java.rmi.Remote;
import java.util.Properties;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

public final class ClientHelper {

	public static final String SERVER_NAME = "//localhost:1099/c4";
	
	
	public static Properties GetNewDisplay() {
		Properties display = new Properties();

		display.setProperty(Constants.PIN, "");
		display.setProperty(Constants.NAME, "");
		display.setProperty(Constants.DOB, "");
		display.setProperty(Constants.SSN, "");
		display.setProperty(Constants.STREET, "");
		display.setProperty(Constants.ZIP, "");
		display.setProperty(Constants.CITY, "");
		display.setProperty(Constants.STATE, "");
		display.setProperty(Constants.VOICEMAIL, "");
		display.setProperty(Constants.CALLER_ID, "");
		display.setProperty(Constants.THREE_WAY, "");
		display.setProperty(Constants.DISTANCE_PROVIDER, "");
		display.setProperty(Constants.ACCOUNT_NUMBER, "");
		display.setProperty(Constants.PHONE0, "");
		display.setProperty(Constants.PHONE1, "");
		display.setProperty(Constants.PHONE2, "");
		display.setProperty(Constants.PHONE3, "");

		display.setProperty(Constants.TROUBLE0, "");
		display.setProperty(Constants.TROUBLE1, "");
		display.setProperty(Constants.TROUBLE2, "");
		display.setProperty(Constants.TROUBLE3, "");

		display.setProperty(Constants.SERVICE0, "");
		display.setProperty(Constants.SERVICE1, "");
		display.setProperty(Constants.SERVICE2, "");
		display.setProperty(Constants.SERVICE3, "");

		return display;
	}

	public static Properties InitializeDisplay(String CustomerID, String PIN)
	{
		Properties display = GetNewDisplay();

		System.setProperty("java.security.policy", "c4.policy");       

       try
       {
		    Server transactionManager = (Server)Naming.lookup("rmi:" + SERVER_NAME);
			Worker tmWorker = (Worker)transactionManager.getWorker();

			//Customer Login Task
			Task tmTask = new Task(Constants.CUSTOMER_LOGIN);
			tmTask.addParameter(Constants.PIN, PIN);
			tmTask.addParameter(Constants.ACCOUNT_NUMBER, CustomerID);

			AccountRepresentation accRep = tmWorker.request(tmTask);

			if(accRep.getSuccessful())
			{
				display.putAll(accRep.getUpdates());
			}		
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return display;
	}

	public static Properties CreateCustomer(Properties accRep, JPanel parent)
	{
		Worker worker;
		TransactionManager manager;
		Task task;
		AccountRepresentation representation;

		Properties display = GetNewDisplay();
		
		// Create a new customer
		task = new Task(Constants.NEW_CUSTOMER_ACCOUNT);
		task.addParameter(Constants.PIN, accRep.getProperty(Constants.PIN));
		task.addParameter(Constants.NAME, accRep.getProperty(Constants.NAME));
		//task.addParameter(Constants.DOB, accRep.getProperty(Constants.DOB));
		task.addParameter(Constants.DOB, "841114");
		task.addParameter(Constants.SSN, accRep.getProperty(Constants.SSN));
		task.addParameter(Constants.STREET, accRep.getProperty(Constants.STREET));
		task.addParameter(Constants.CITY, accRep.getProperty(Constants.CITY));
		task.addParameter(Constants.ZIP, accRep.getProperty(Constants.ZIP));
		task.addParameter(Constants.STATE, accRep.getProperty(Constants.STATE));
		task.addParameter(Constants.VOICEMAIL, accRep.getProperty(Constants.VOICEMAIL));
		task.addParameter(Constants.CALLER_ID, accRep.getProperty(Constants.CALLER_ID));
		task.addParameter(Constants.THREE_WAY, accRep.getProperty(Constants.THREE_WAY));
		task.addParameter(Constants.DISTANCE_PROVIDER, accRep.getProperty(Constants.DISTANCE_PROVIDER));

		System.setProperty("java.security.policy", "c4.policy");       

       try
       {
		    Server transactionManager = (Server)Naming.lookup("rmi:" + SERVER_NAME);
			worker = (Worker)transactionManager.getWorker();
	
			representation = worker.request(task);
	
			if (representation.getSuccessful()) { // everything was Ok, so display
				display.putAll(representation.getUpdates()); // load all updated
																// values
				display.list(System.out); // display with updates
			}
	
			else {// there was an error with the request, so errors should be
					// displayed
				JOptionPane.showMessageDialog(parent, representation.getErrorMessages());
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return display;
	}
	
	public static Properties ActivateService(Properties accRep, JPanel parent)
	{
		Worker worker;
		Task task;
		AccountRepresentation representation;

		Properties display = GetNewDisplay();
		
		// Create a new customer
		task = new Task(Constants.ACTIVATE_PHONE_SERVICE);
		task.addParameter(Constants.ACCOUNT_NUMBER, accRep.getProperty(Constants.ACCOUNT_NUMBER));
		task.addParameter(Constants.PIN, accRep.getProperty(Constants.PIN));
		task.addParameter(Constants.STREET, accRep.getProperty(Constants.STREET));
		task.addParameter(Constants.CITY, accRep.getProperty(Constants.CITY));
		task.addParameter(Constants.ZIP, accRep.getProperty(Constants.ZIP));
		task.addParameter(Constants.STATE, accRep.getProperty(Constants.STATE));
		
		System.setProperty("java.security.policy", "c4.policy");       

       try
       {
		    Server transactionManager = (Server)Naming.lookup("rmi:" + SERVER_NAME);
			worker = (Worker)transactionManager.getWorker();
	
			representation = worker.request(task);
	
			if (representation.getSuccessful()) { // everything was Ok, so display
				display.putAll(representation.getUpdates()); // load all updated
																// values
				display.list(System.out); // display with updates
			}
	
			else {// there was an error with the request, so errors should be
					// displayed
				JOptionPane.showMessageDialog(parent, representation.getErrorMessages());
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return display;
	}

	public static Properties CustomerLogin(Properties accRep, JPanel parent)
	{
		System.setProperty("java.security.policy", "c4.policy");
		// System.setSecurityManager(new RMISecurityManager());

		Worker worker;
		Task task;
		AccountRepresentation representation;
		Properties display = GetNewDisplay();
		
//				System.setProperty("java.security.policy", "c4.policy");       

       try
       {
		    Server transactionManager = (Server)Naming.lookup("rmi:" + SERVER_NAME);
			worker = (Worker)transactionManager.getWorker();
			
			task = new Task(Constants.CUSTOMER_LOGIN);
			task.addParameter(Constants.PIN, "1234");
			task.addParameter(Constants.ACCOUNT_NUMBER, "1");

			representation = worker.request(task);

			if (representation.getSuccessful()) { // everything was Ok, so
													// display
				display.putAll(representation.getUpdates()); // load all
																// updated
																// values
			}

			else
				// there was an error with the request, so errors should be
				// displayed
				JOptionPane.showMessageDialog(parent, representation.getErrorMessages());

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return display;
	}

	public static Properties CustomerLogout(String CustomerID)
	{
		Worker worker;
		Task task;
		AccountRepresentation representation;

		Properties accRep = GetNewDisplay();

		// Create a new customer
		task = new Task(Constants.CUSTOMER_LOGOUT);
		task.addParameter(Constants.ACCOUNT_NUMBER, CustomerID);

		System.setProperty("java.security.policy", "c4.policy");       

   try
   {
	    Server transactionManager = (Server)Naming.lookup("rmi:" + SERVER_NAME);
		worker = (Worker)transactionManager.getWorker();

		representation = worker.request(task);

		if (representation.getSuccessful()) { // everything was Ok, so display
			if (representation.getUpdates() != null)
				accRep.putAll(representation.getUpdates()); // load all
																// updated
																// values
		}

		else
			// there was an error with the request, so errors should be
			// displayed
			System.out.println(representation.getErrorMessages()); // display
																	// error
		}catch (Exception e){}
		
		return accRep;
	}

	public static Properties NewServiceRequest(Properties displayCustomer, String Description, JPanel parent) {
		Worker worker;
		Task task;
		AccountRepresentation representation;
		
		
		// display.list(System.out);
		// System.out.println("\n---------------\n");
	
		// Create a new customer
		task = new Task(Constants.REQUEST_INSTALLATION);
		task.addParameter(Constants.ACCOUNT_NUMBER, displayCustomer.getProperty(Constants.ACCOUNT_NUMBER));
		task.addParameter(Constants.PIN, displayCustomer.getProperty(Constants.PIN));
		task.addParameter(Constants.STREET, displayCustomer.getProperty(Constants.STREET));
		task.addParameter(Constants.CITY, displayCustomer.getProperty(Constants.CITY));
		task.addParameter(Constants.ZIP, displayCustomer.getProperty(Constants.ZIP));
		task.addParameter(Constants.STATE, displayCustomer.getProperty(Constants.STATE));
		task.addParameter(Constants.DESCRIPTION, Description);
		
		System.setProperty("java.security.policy", "c4.policy");       

       try
       {
		    Server transactionManager = (Server)Naming.lookup("rmi:" + SERVER_NAME);
			worker = (Worker)transactionManager.getWorker();
			
			representation = worker.request(task);
		
		
			if (representation.getSuccessful()) { // everything was Ok, so display
				if (representation.getUpdates() != null)
					displayCustomer.putAll(representation.getUpdates()); // load all
																	// updated
																	// values
			}
	
			else
				// there was an error with the request, so errors should be
				// displayed
				JOptionPane.showMessageDialog(parent, representation.getErrorMessages());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	return displayCustomer;
	}

	public static Properties NewTroubleTicket(Properties displayCustomer, String Description, JPanel parent)
	{
		Worker worker;
		Task task;
		AccountRepresentation representation;

		// display.list(System.out);
		// System.out.println("\n---------------\n");

		// Create a new customer
		task = new Task(Constants.CREATE_TROUBLE_TICKET);
		task.addParameter(Constants.ACCOUNT_NUMBER, displayCustomer.getProperty(Constants.ACCOUNT_NUMBER));
		task.addParameter(Constants.PIN, displayCustomer.getProperty(Constants.PIN));
		task.addParameter(Constants.STREET, displayCustomer.getProperty(Constants.STREET));
		task.addParameter(Constants.CITY, displayCustomer.getProperty(Constants.CITY));
		task.addParameter(Constants.ZIP, displayCustomer.getProperty(Constants.ZIP));
		task.addParameter(Constants.STATE, displayCustomer.getProperty(Constants.STATE));
		task.addParameter(Constants.DESCRIPTION, Description);
		
	System.setProperty("java.security.policy", "c4.policy");       

   try
   {
	    Server transactionManager = (Server)Naming.lookup("rmi:" + SERVER_NAME);
		worker = (Worker)transactionManager.getWorker();

		representation = worker.request(task);

		if (representation.getSuccessful()) { // everything was Ok, so display
			if (representation.getUpdates() != null)
				displayCustomer.putAll(representation.getUpdates()); // load all
																// updated
																// values
			displayCustomer.list(System.out); // display with updates
		}

		else
			// there was an error with the request, so errors should be
			// displayed
			JOptionPane.showMessageDialog(parent, representation.getErrorMessages());
																	// error
		}catch (Exception e){}
		
		return displayCustomer;
	}
}
